﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.Mvc;
//using www.SService;


namespace www.Controllers
{
    public class MenuControllers : Controller
    {
        private readonly ObjectCache _cache = MemoryCache.Default;
        //private readonly ServiceClient _service1 = new ServiceClient();
        ////
        //// GET: /Menu/
        
        //public ActionResult Index()
        //{
        //    //var lData = new List<RelaxMenuDataContract>();
        //    if (_cache["Menu"] == null)
        //    {
        //        var lData = _service1.GetMenuDataContracts();
        //        if (lData.Any())
        //        {
        //            var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) };

        //            _cache.Set("Menu", lData, policy);
        //            ViewData["Data"] = lData.Where(p => p.Active == 1).ToList();
        //        }
        //    }
        //    else
        //    {
        //       var lData = (List<RelaxMenuDataContract>)_cache["Menu"];

        //       ViewData["Data"] = lData.Where(p =>  p.Active == 1).ToList();
        //    }
        //    return View();
        //}
     
        //private void LoadLinkType(string target = null)
        //{
        //    var linkType = new List<SelectListItem>
        //    {
        //        new SelectListItem {Text = "_self", Value = "_self",Selected = target=="_self"},
        //        new SelectListItem {Text = "_blank", Value = "_blank",Selected = target=="_blank"},
        //        new SelectListItem {Text = "_parent", Value = "_parent",Selected = target=="_parent"},
        //        new SelectListItem {Text = "_top", Value = "_top",Selected = target=="_top"}
        //    };
        //    ViewBag.LinkType = linkType;
        //}

        //[HttpGet]
        //public ActionResult EditMenu(int id=0)
        //{
        //    var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
        //    var first = lData.FirstOrDefault(p => p.Id == id);
        //    if (first != null)
        //        LoadLinkType(first.Target);
        //    return View(first);
        //}

        //[HttpPost]
        //public ActionResult EditMenu(FormCollection collection)
        //{
        //    var id = collection["id"];
        //    var ord = collection["txtOrd"];
        //    var link = collection["txtLink"];
        //    var linktype = collection["LinkType"];
        //    var active = collection["chkActive"].Split(',').Length>1?1:0;
        //    if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(ord) || string.IsNullOrEmpty(link) ||
        //        string.IsNullOrEmpty(linktype))
        //        return View();

        //    var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
        //    var first = lData.FirstOrDefault(p => p.Id == Convert.ToInt32(id));
        //    if (first == null)
        //    {
        //        Response.Write("<script>alert('Menu chưa được cập nhật');</script>");
        //        return View();
        //    }
        //    first.Id = Convert.ToInt32(id);
        //    first.Ord = Convert.ToInt32(ord);
        //    first.Link = link;
        //    first.Target = linktype;
        //    first.Active = active;
        //    //_service1.UpdateMenu(first);
        //    _cache["Menu"] = lData;
        //    return RedirectToAction("Index","Menu");
        //}

        //[HttpGet]
        //public ActionResult DeleteMenu(int id)
        //{
        //    var lId = new List<int> {id};

        //    //var ok = _service1.DeleteMenu(lId);
        //    //if (ok)
        //    //{
        //    //    var lData = (List<RelaxMenuDataContract>)_cache["Menu"];
        //    //    var first = lData.FirstOrDefault(p => p.Id == id);
        //    //    if (first != null)
        //    //        lData.Remove(first);
        //    //    _cache["Menu"] = lData;
        //    //}
        //    return RedirectToAction("Index", "Menu");
        //}

    }
}
